package com.fz.us.oa.web.controller.main;

import com.fz.us.admin.company.entity.*;
import com.fz.us.admin.company.service.*;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.oa.core.entity.com.NoticeTest;
import com.fz.us.oa.core.entity.common.FileManage;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 控制器 - 主页
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-01 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main")
public class IndexController extends BaseController {

    private static final long serialVersionUID = -3387066815980698553L;

    @Autowired
    private ProcessBriefService processBriefService;
    @Autowired
    private StepBriefService stepBriefService;
    @Autowired
    private CommonConfigService commonConfigService;
    @Autowired
    private TaskEntityService taskEntityService;
    @Autowired
    private TaskFlowService taskFlowService;
    @Autowired
    private DutyService dutyService;

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(@CurrentUser User user, Model model) {

        model.addAttribute("user", user);

        return "/main/index";
    }

    /**
     * 计算操作者可选身份
     * @return
     */
    @RequestMapping(value = "dutyDlg", method = RequestMethod.GET)
    public Object dutyDlg(@CurrentUser User user,String entityName,String keyId){
        Map<String,Object> rMap=new HashMap<String,Object>();
        rMap.put("state","400");
        if(StringUtils.isEmpty(entityName)){
            return ajaxJson(rMap);
        }
        ProcessBrief processBrief = processBriefService.getInfoByTableKey(entityName);
        if(processBrief == null ){
            return ajaxJson(rMap);
        }
        StepBrief stepBrief = null;
        List<Duty> dutyList = null;
        if(StringUtils.isEmpty(keyId)){
            stepBrief = stepBriefService.getInfoByStepNo(processBrief, 1);
            dutyList = commonConfigService.findDutyByConfig(stepBrief.getCommonConfig().getId());
        }else{
            TaskEntity taskEntity = taskEntityService.getInfoByKeyId(keyId);
            if(taskEntity == null){
                stepBrief = stepBriefService.getInfoByStepNo(processBrief, 1);
            }else{
                stepBrief = stepBriefService.getInfoByStepNo(processBrief, taskEntity.getStepNo());
            }
            dutyList = commonConfigService.findDutyByConfig(keyId, entityName, stepBrief.getCommonConfig().getId());
            //获取上一步的特殊职责
            if(StringUtils.isNotEmpty(stepBrief.getSpecialType())){
                List<TaskFlow> taskFlowList = taskFlowService.getInfoByTaskStepBrief(taskEntity, stepBrief);
                for(TaskFlow taskFlow: taskFlowList){
                    Set<Duty> specialDutySet = stepBriefService.findSpecialByDuty(stepBrief, taskFlow.getDuty());
                    dutyList.addAll(specialDutySet);
                }
            }
        }
        if(stepBrief == null){
            return ajaxJson(rMap);
        }
        dutyList.retainAll(dutyService.getDutys(user));
        Set<Duty> dutySet = Sets.newHashSet();
        dutySet.addAll(dutyList);
        Map<String, Object> data = new HashMap<String, Object>();
        List<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();

        for(Duty duty :dutySet){
            rMap = Maps.newHashMap();
            rMap.put("name",duty.getPower().getDepartment().getName()+"-"+duty.getPower().getPost().getName());
            rMap.put("id",duty.getId());
            rMap.put("checked","");
            dataRows.add( rMap);
        }
        data.put("data",dataRows);
        data.put("size",dataRows.size());
        data.put("state","1");
        data.put("keyId", keyId);
        data.put("entityName", entityName);
        return ajaxJson(data);
    }

    /**
     * 获取身份数据
     */
    @RequestMapping(value = "getDutyDlg", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String getDutyDlg(@CurrentUser User user, Model model,String entityName,String keyId) {

        ProcessBrief processBrief = processBriefService.getInfoByTableKey(entityName);

        StepBrief stepBrief = null;
        List<Duty> dutyList = null;
        if(StringUtils.isEmpty(keyId)){
            stepBrief = stepBriefService.getInfoByStepNo(processBrief, 1);
            dutyList = commonConfigService.findDutyByConfig(stepBrief.getCommonConfig().getId());
        }else{
            TaskEntity taskEntity = taskEntityService.getInfoByKeyId(keyId);
            if(taskEntity == null){
                stepBrief = stepBriefService.getInfoByStepNo(processBrief, 1);
            }else{
                stepBrief = stepBriefService.getInfoByStepNo(processBrief, taskEntity.getStepNo());
            }
            dutyList = commonConfigService.findDutyByConfig(keyId, entityName, stepBrief.getCommonConfig().getId());
            //获取上一步的特殊职责
            if(StringUtils.isNotEmpty(stepBrief.getSpecialType())){
                List<TaskFlow> taskFlowList = taskFlowService.getInfoByTaskStepBrief(taskEntity, stepBrief);
                for(TaskFlow taskFlow: taskFlowList){
                    Set<Duty> specialDutySet = stepBriefService.findSpecialByDuty(stepBrief, taskFlow.getDuty());
                    dutyList.addAll(specialDutySet);
                }
            }
        }
        dutyList.retainAll(dutyService.getDutys(user));
        model.addAttribute("entityName",entityName);
        model.addAttribute("keyId",keyId);
        model.addAttribute("dutyList",dutyList);
        return "/main/getDutyDlg_input";
    }

}
